//
//  SinaVisitorView.swift
//  SinaWeibo
//
//  Created by 王朝阳 on 2016/10/24.
//  Copyright © 2016年 wangzhaoyang. All rights reserved.
//

import UIKit

class SinaVisitorView: UIView {
    
    //MARK: - 定义闭包
    var closure:(()->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 监听点击事件
    @objc private func loginClick() {
        //执行闭包
        closure?()
        
    }
    //MARK: - 创建不同的视图,因为其他页面都是参照首页进行创建的
    func setupVisitorViewInfo(imageName: String?, title: String?) {
        //如果 imageName 和 title 为 nil, 则是,消息 发现 我
        if let img = imageName, let tit = title {
            iconImageView.image = UIImage(named:  img)
            messageLabel.text = tit
            circleImageView.isHidden = true
        }
        //代表是首页,则不需要在设置了
        setupCircleImageViewAnim()
    }
    
    //MARK: - 设置圆圈的动画
    private func setupCircleImageViewAnim() {
        //核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        //动画的角度
        anim.toValue = M_PI * 2
        //动画的时长
        anim.duration = 20
        //重复的次数
        anim.repeatCount = MAXFLOAT
        
        //完成以后 是否移除动画
        anim.isRemovedOnCompletion = false
        
        //添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    
    //MARK: - 创建视图
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237/255, alpha: 1)
            //添加控件
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //添加约束
        //圆圈
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //遮罩
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //logo
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //文字
        messageLabel.snp_makeConstraints { (make) in
            make.width.equalTo(230)
            make.centerX.equalTo(self)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        //登录按钮
        loginButton.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
    }
    
    //MARK: - 懒加载控件
    //圆圈
    fileprivate lazy var circleImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    //遮罩
    fileprivate lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    //logo
    fileprivate lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    //文字
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: SinaNormalFontSize, textColor: UIColor.darkGray)
        //设置文字
//        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
//        label.font = UIFont.boldSystemFont(ofSize: 14)
//        label.textColor = UIColor.darkGray
        //对齐方式
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    //注册按钮
    fileprivate lazy var registerButton: UIButton = {
       
        let button = UIButton(setBackImgName: "common_button_white", title: "注册", fontSize: SinaNormalFontSize, titleColor: SinaColor, target: self, action: #selector(loginClick))
        return button
    }()
    //登录按钮
    fileprivate lazy var loginButton: UIButton = {
        
        let button = UIButton(setBackImgName: "common_button_white", title: "登录", fontSize: SinaNormalFontSize, titleColor: SinaColor, target: self, action: #selector(loginClick))
        return button
    }()
}
